/************************************************************************************
       ***********LD_Interface文件主要编写了LD3320底层配置的相关接口函数***********
 					 LD3320 有源晶振 22MHZ  不同的晶振请修改LDChip.h 中的定义值（＃define CLK_IN  22）
************************************************************************************/
#include  "ld_interface.h"

void LD3320_SPI_Config(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;
  	SPI_InitTypeDef  SPI_InitStructure;

  	SPIx_APBxClock(SPIx_CLK, ENABLE);//使能SPI1
  	SPIx_GPIO_APBxClock(SPIx_GPIO_CLK,ENABLE);//打开GPIOA时钟
	/*SPI硬件引脚*/
  	GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN|SPIx_MOSI_PIN;//PA5和PA7
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	 //复用推挽输出
  	GPIO_Init(SPIx_GPIO_PORT, &GPIO_InitStructure); //初始化GPIOA
		GPIO_SetBits(SPIx_GPIO_PORT,SPIx_SCK_PIN|SPIx_MOSI_PIN);	
	  
		GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;//PA6
		GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;//浮空输入
		GPIO_Init(SPIx_GPIO_PORT, &GPIO_InitStructure); //初始化GPIOA
		
	/*软件驱动片选引脚*/
		GPIO_InitStructure.GPIO_Pin = SPIx_CS_PIN; 				//PA4
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
		GPIO_Init(SPIx_GPIO_PORT, &GPIO_InitStructure);	  //初始化GPIOA
		SPIx_CS_HIGH();

	/*SPI结构体的初始化*/	
		SPI_Cmd(SPIx, DISABLE);
		
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//全双工
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//设置SPI工作模式：主机模式
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//设置SPI数据大小：8位帧结构
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High ;//根据LD3320时序,串行同步时钟空闲时SCLK位高电平
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//根据LD3320时序,串行同步时钟空第一个时钟沿捕获
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//NSS信号由软件管理
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;//LD3320传输速度小于1.5M,故波特率预分频值为64
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//数据传输高位先行
    SPI_InitStructure.SPI_CRCPolynomial = 7;//CRC值计算的多项式
	/*开启SPI1*/
    SPI_Init(SPIx,&SPI_InitStructure);//初始化SPI
    SPI_Cmd(SPIx, ENABLE);//使能SPI
}
void LD3320_GPIO_Config(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		/* Enable GPIOB clock */
		LD3320_GPIO_APBxClock(LD3320_GPIO_CLK, ENABLE);
		GPIO_InitStructure.GPIO_Pin = LD3320_RES_PIN|LD3320_WR_PIN;//PB15(复位)和PB13(写允许)
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(LD3320_GPIO_PORT, &GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Pin=LD3320_IT_PIN;								//PB12,用于中断
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(LD3320_GPIO_PORT, &GPIO_InitStructure);
}
static unsigned char SPI_Send_Byte(uint8 byte)
{
	uint16 time=0;
	 while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)
	{
			if(time++>0xffff)
	 {
		 UsartPrintf(USART1,"SPI发送数据失败\r\n");	
		return 0;
	 }
	}
	SPI_I2S_SendData(SPIx,byte);
	time=0;
	while (SPI_I2S_GetFlagStatus(SPIx,SPI_I2S_FLAG_RXNE) == RESET)
	{
		if(time++>0xffff)
	 {
		 UsartPrintf(USART1,"SPI接收数据失败\r\n");	
		return 0;
	 }
	}
	return SPI_I2S_ReceiveData(SPIx);
}
void LD_WriteReg(unsigned char address,unsigned char dataout)
{
	SPIx_CS_LOW();//LD3320作为从机
	LD3320_WR_LOW();//LD3320写允许
	SPI_Send_Byte(0x04);
	SPI_Send_Byte(address);
	SPI_Send_Byte(dataout);
  SPIx_CS_HIGH();//关闭LD3320被SPI选为从机
}
		
uint8 LD_ReadReg(unsigned char address)
{
	uint8 dat;

	SPIx_CS_LOW();//LD3320作为从机
	LD3320_WR_LOW();//LD3320写允许
	SPI_Send_Byte(0x05);
	SPI_Send_Byte(address);
	dat=SPI_Send_Byte(0x00);
	SPIx_CS_HIGH();//关闭LD3320被SPI选为从机
	return dat;
}



